<template>
	<view class="body">
		<view class="no-charts" v-if="isExists==0">
			<h3>暂无数据</h3>
		</view>
		<view v-else>
			<uni-card title="年度消费">
				<text style="font-weight: bold;">{{dateFormat(date)}}年&nbsp;累计消费¥{{totalAllPrice}}</text>
			</uni-card>
			<view class="charts-box">
				<qiun-data-charts type="column" :opts="opts" :chartData="chartData" />
			</view>
			<view v-if="totalAllPrice<10000">
				<h2 class="title1">本年度消费没有超标！继续保持！</h2>
			</view>
			<view v-else-if="totalAllPrice>10000&&totalAllPrice<99999">
				<h2 class="title2">本年度消费已经超标！请克制！</h2>
			</view>
			<view v-else>
				<h2 class="title3">为了自己好！请立即停止消费！</h2>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				date: new Date().toISOString(),
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['column'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 15, 0, 5],
					legend: {},
					xAxis: {
						disableGrid: true
					},
					yAxis: {
						data: [{
							min: 0
						}]
					},
					extra: {
						column: {
							type: "group",
							width: 30,
							activeBgColor: "#000000",
							activeBgOpacity: 0.09
						}
					}
				},
				timeList: [],
				priceList: [],
				isExists: 0,//是否存在账单数据
				totalAllPrice: 0
			};
		},
		onReady() {
			this.getOrders();
		},
		methods: {
			dateFormat(time) {
				let date = new Date(time);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
				return year;
			},
			getOrders() {
				uni.request({
					url: '/api/order/selOrders',
					data: {
						userId: this.$store.getters.getId
					},
					method: 'POST',
					success: (res => {
						if (res.data.length) {
							this.isExists = 1 //判断
							this.timeList = res.data.map((item, index) => {
								let m = item.m;
								return m;
							});
							this.priceList = res.data.map((item, index) => {
								let totalPrices = item.totalPrices;
								return totalPrices;
							});
							//获取总账单
							this.priceList.map((item, index) => {
								this.totalAllPrice += item
							})
							let orderData = {
								categories: this.timeList,
								series: [{
									name: "每月消费/元",
									data: this.priceList
								}]
							};
							this.chartData = JSON.parse(JSON.stringify(orderData));
						} else {
							this.isExists = 0
						}
					})
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.charts-box {
		width: 100%;
		height: 300px;
	}

	.no-charts {
		position: absolute;
		top: 50%;
		left: 50%;
		-webkit-transform: translate(-50%, -50%);
		transform: translate(-50%, -50%);
	}

	.title1 {
		text-align: center;
		margin-top: 100px;
		color: #18B566;
	}
	.title2 {
		text-align: center;
		margin-top: 100px;
		color: #F29100;
	}
	.title3 {
		text-align: center;
		margin-top: 100px;
		color: #F23030;
	}
</style>
